package com.pay.game.central.web.mapper;

import com.github.pagehelper.Page;
import com.pay.game.central.web.bean.GroupOrderId;
import com.pay.game.central.web.bean.PddGoods;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface GoodsMapper {

    @Insert("insert into pdd_goods(order_index,id,goods_name,price,thumb_url,link_url," +
            "store_id,update_time,`loop`,sku_id,group_id,group_id1,price1)" +
            " values(#{order_index,jdbcType=INTEGER},#{id},#{goods_name},#{price,jdbcType=INTEGER}," +
            "#{thumb_url},#{link_url},#{store_id},#{update_time,jdbcType=BIGINT},true," +
            "#{sku_id},#{group_id},#{group_id1},#{price1,jdbcType=INTEGER})")
    void addGoods(
            @Param("order_index") int order_index,
            @Param("id") String id,
            @Param("goods_name") String goodsName,
            @Param("price") int price,
            @Param("thumb_url") String thumbUrl,
            @Param("link_url") String linkUrl,
            @Param("store_id") String storeId,
            @Param("sku_id") String skuId,
            @Param("group_id") String groupId,
            @Param("group_id1") String groupId1,
            @Param("price1") int price1,
            @Param("update_time")long update_time);

    @Insert("insert into pdd_goods_group_id(id, goods_id, times) values(#{id}, #{goods_id}, #{times,jdbcType=INTEGER})")
    void addGroupOrderId(
            @Param("goods_id") String goodsId,
            @Param("id") String id,
            @Param("times") int times
    );

    @Delete("delete from pdd_goods_group_id where goods_id in (select id from pdd_goods where store_id = #{storeId})")
    void deleteGroupOrderId(@Param("storeId") String storeId);

    @Update("update pdd_goods_group_id set times=times+1 where id = #{id}")
    void increaseGroupOrderIdTimes(@Param("id") String id);

    @Select("select id,times from pdd_goods_group_id where goods_id=#{goods_id} order by times")
    List<GroupOrderId> getGroupOrderId(@Param("goods_id") String goodsId);

    @Select("select id,times from pdd_goods_group_id where id = #{id}")
    GroupOrderId getGroupOrderIdById(@Param("id") String id);

    @Update("update pdd_goods set goods_name=#{goods_name},price=#{price,jdbcType=INTEGER}," +
            "sku_id=#{sku_id}, group_id=#{group_id},group_id1=#{group_id1},price1=#{price1,jdbcType=INTEGER}, " +
            "  thumb_url=#{thumb_url},link_url=#{link_url},store_id=#{store_id},update_time=#{update_time,jdbcType=BIGINT} " +
            "where id=#{id}")
    void updateGoods(PddGoods goods);

    @Update("update pdd_goods set group_order_id = #{groupOrderId} where id = #{id}")
    void setGroupOrderId(@Param("id")String id, @Param("groupOrderId")String groupOrderId);

    @Update("update pdd_goods set `loop`=#{loop,jdbcType=BOOLEAN} where id=#{id}")
    void setLoop(@Param("id")String id,@Param("loop")boolean loop);

    @Update("update pdd_goods set sku_id=#{sku_id},group_id=#{group_id},price=#{price,jdbcType=INTEGER} where id=#{id}")
    void setGoodsSkuAndGroupAndPrice(@Param("id")String id,
                                     @Param("sku_id")String skuId,
                                     @Param("group_id")String groupId,
                                     @Param("price")int price);

    @Update("update pdd_goods set order_time=#{time,jdbcType=BIGINT} where id=#{id}")
    void setOrderTime(@Param("id")String id,@Param("time")long orderTime);

    @Update("update pdd_goods set loop_times=loop_times+1 where id=#{id}")
    void increaseLoopTime(@Param("id")String id);

    @Update("update pdd_goods set done_times=done_times+1,pay_time=#{payTime,jdbcType=INTEGER} where id=#{id}")
    void increaseDoneTimeAndSetPayTime(@Param("id")String id, @Param("payTime") int payTime);

    @Select("select count(id) from pdd_goods")
    int countGoods();

    @Select("select * from pdd_goods where id=#{id}")
    PddGoods queryById(@Param("id") String id);

    @Select("select id from pdd_goods where sku_id is null order by order_index limit 1")
    String getFirstId();

    @Select("select id from pdd_goods,(select order_index from pdd_goods where id=#{id}) g1 where pdd_goods.order_index=g1.order_index+1")
    String queryNextId(@Param("id") String currentId);

    /**
     * 将商店名放在在store_id中
     * @return 所有货物带商店名
     */
    @Select("select g.order_index,g.id,g.goods_name,g.price,g.thumb_url,g.sku_id,g.group_id, s.name store_id  from pdd_goods g,pdd_store s where g.store_id=s.id")
    List<PddGoods> getAllGoodsWithStoreName();

    @Select("select id,sku_id,group_id,price from pdd_goods where price=#{price,jdbcType=INTEGER}")
    List<PddGoods> getGoodsByPrice(@Param("price") int price);

    @Select("<script> " +
            "   select pdd_goods.*, sys_user.name user_name, pdd_store.name store_name," +
            "   (select count(id) from `order` po where po.goods_id=pdd_goods.id and  pay_time>#{today,jdbcType=BIGINT}) today_num" +
            "   from pdd_goods " +
            "   join pdd_store on pdd_goods.store_id=pdd_store.id " +
            "   left join sys_user on pdd_store.user_id=sys_user.id " +
            "   where 1=1 " +
            "       <if test=\"id != null and id != ''\">" +
            "         and pdd_goods.id like concat(#{id},'%') " +
            "       </if>"+
            "       <if test=\"store_id != null and store_id != ''\">" +
            "         and pdd_goods.store_id like concat(#{store_id},'%') " +
            "       </if>"+
            "       <if test=\"store_name != null and store_name != ''\">" +
            "         and pdd_store.name like concat(#{store_name},'%') " +
            "       </if>"+
            "       <if test=\"price != 0\">" +
            "         and pdd_goods.price=#{price,jdbcType=INTEGER} " +
            "       </if>"+
            "       <if test=\"goods_name != null and goods_name != ''\">" +
            "         and pdd_goods.goods_name like concat(#{goods_name},'%') " +
            "       </if>" +
            "       <if test=\"user_name != null and user_name != ''\">" +
            "         and sys_user.username like concat(#{user_name},'%') " +
            "       </if>"+
            "</script>")
    Page<Map<String,Object>> list(@Param("id") String id,
                                  @Param("store_id") String store_id,
                                  @Param("store_name") String store_name,
                                  @Param("price") int price,
                                  @Param("goods_name") String goods_name,
                                  @Param("user_name") String user_name,
                                  @Param("today") long todayMill);

    @Select("select pg.id goods_id,pg.price,pg.group_id,pg.sku_id,pg.order_time,pg.store_id,ps.score,ps.used_score,pg.pay_time,ps.order_limit,ps.user_id" +
            "    from pdd_goods pg" +
            "    left join pdd_store ps on pg.store_id = ps.id" +
            "    where pg.`loop`=true and ps.`loop`=true " +
            "      and pg.price=#{price,jdbcType=INTEGER} " +
            "      order by pg.order_time")
    List<Map<String,Object>> getOrderGoodsByPrice(@Param("price") int price);

    @Select("select pg.id goods_id,pg.price,pg.group_id,pg.sku_id,pg.order_time,su.order_limit," +
            "pg.store_id,ps.score,ps.used_score,pg.pay_time,ps.order_limit done_size,ps.user_id" +
            "    from pdd_goods pg" +
            "    left join pdd_store ps on pg.store_id = ps.id" +
            "    left join sys_user su on ps.user_id = su.id" +
            "    where pg.`loop`=true and ps.`loop`=true " +
            "    and su.`pause`=1 " +
            "    and pg.price>=#{ltPrice, jdbcType=INTEGER} " +
            "    and pg.price<=#{price, jdbcType=INTEGER} " +
            "    and ps.user_id like concat(#{userId},'%') " +
            "    order by pg.order_time")
    List<Map<String,Object>> getOrderGoods(@Param("ltPrice") int ltPrice, @Param("price") int price, @Param("userId") String userId);


    @Select("<script> " +
            "   select ps.id, sum(pg.done_times) done_times from pdd_store ps join pdd_goods pg on pg.store_id = ps.id " +
            "   where ps.id in " +
            "       <foreach collection='ids' item='id' index='index' " +
            "              open='(' close=')' separator=','> " +
            "          #{id} " +
            "       </foreach>" +
            "   group by ps.id " +
            "</script>")
    List<Map<String, Object>> getStoreDonSize(@Param("ids") List<String> ids);

    @Select("select pg.id goods_id,pg.price1 price,pg.group_id1 group_id,pg.sku_id,pg.order_time," +
            "su.order_limit," +
            "pg.store_id,ps.score,ps.used_score,pg.pay_time,ps.order_limit done_size,ps.user_id" +
            "    from pdd_goods pg" +
            "    left join pdd_store ps on pg.store_id = ps.id" +
            "    left join sys_user su on ps.user_id = su.id" +
            "    where pg.`loop`=true and ps.`loop`=true " +
            "    and su.`pause`=1 " +
            "    and pg.price1>=#{ltPrice, jdbcType=INTEGER} " +
            "    and pg.price1<=#{price, jdbcType=INTEGER} " +
            "    and ps.user_id like concat(#{userId},'%') " +
            "    order by pg.order_time")
    List<Map<String,Object>> getOrderGoodsPd(@Param("ltPrice") int ltPrice, @Param("price") int price, @Param("userId") String userId);



    @Select("select ps.settlement_id from pdd_store ps,pdd_goods pg " +
            "where pg.id=#{id} and ps.id=pg.store_id")
    long getCurrentSettlementID(@Param("id") String id);

    @Select("select pg.goods_name, ps.settlement_id, pg.store_id, ps.name store_name, su.id user_id, su.name user_name  " +
            "   from pdd_goods pg " +
            "   join pdd_store ps on pg.store_id = ps.id " +
            "   left join sys_user su on ps.user_id = su.id " +
            "where pg.id = #{id}")
    Map<String, Object> getGoodsDetailInfo(@Param("id") String goodsId);

}
